package com.digitalchina.lxnsy.activity.old;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.api.BusinessExpert;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.entity.MyExpertVO;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.toolkit.GotoUtil;
import com.digitalchina.lxnsy.viewholder.MyExpertsListHolder;
import com.digitalchina.lxnsy.xml.LocationXML;
import com.jude.easyrecyclerview.EasyRecyclerView;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;

import android.os.Handler;
import android.os.Message;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.ViewGroup;

import butterknife.Bind;

/**
 * 我的专家 Created by zhy on 2018/1/12 0012.
 */

public class MyExpertListActivity extends BaseActivity implements  RecyclerArrayAdapter.OnLoadMoreListener, SwipeRefreshLayout.OnRefreshListener
{
	@Bind(R.id.recycler_view)
	EasyRecyclerView recyclerView;
	private RecyclerArrayAdapter adapter;
	private int page = 1;
	private int limit = 10;

	private List<MyExpertVO> expertList = new ArrayList<>();

	@Override
	public void setLayout()
	{
        setContentLayout(R.layout.activity_my_expertlist);
        setTitle("专家团队");

	}

	@Override
	public void initMember()
	{
		recyclerView.setLayoutManager(new LinearLayoutManager(this));

//        recyclerView.addItemDecoration(Utils.setSpaceDecoration(this, false, true, false));

		recyclerView.setAdapterWithProgress(adapter = new RecyclerArrayAdapter(this) {
			@Override
			public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
				return new MyExpertsListHolder(parent);
			}
		});
		onRefresh();

	}

	@Override
	public void addListener()
	{
		recyclerView.setRefreshListener(this);

		adapter.setMore(R.layout.view_more, this);

		adapter.setNoMore(R.layout.view_nomore);

		adapter.setError(R.layout.view_error, new RecyclerArrayAdapter.OnErrorListener() {
			@Override
			public void onErrorShow() {
				adapter.resumeMore();
			}

			@Override
			public void onErrorClick() {
				adapter.resumeMore();
			}
		});

		adapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
			@Override
			public void onItemClick(int position) {
				Map<String, Object> map = new HashMap<>();
				map.put("MyExpertVO", adapter.getAllData().get(position));
				GotoUtil.gotoActivity(MyExpertListActivity.this, AddMyExpertsActivity.class, "map", map);
			}
		});
	}

	@Override
	public void setHandler()
	{
		mHandler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				super.handleMessage(msg);
				if (recyclerView == null)
					return;

				if (recyclerView.getSwipeToRefresh().isRefreshing())
					recyclerView.setRefreshing(false);
				switch (msg.what) {
					case MSG.MSG_MYEXPERTS_LIST_SUCCESS:
						callBack(msg.obj);
						break;
					case MSG.MSG_MYEXPERTS_LIST_FIELD:
						if (adapter.getAllData().size() > 0)
							adapter.pauseMore();
						else
							recyclerView.showEmpty();
						break;
				}
			}
		};
	}

	@Override
	public void callBack(Object object)
	{
		expertList = FastJsonUtil.getListBean(object.toString(), "body", "orgList", MyExpertVO.class);

		if (page == 1) { // 刷新
			adapter.clear();
		}

		adapter.addAll(expertList);

		if (expertList != null) {
			if (expertList.size() < limit) {
				adapter.stopMore();
			}
		}

	}

	@Override
	public void rightOnClick()
	{

	}

	@Override
	public void onRefresh() {
		page = 1;
		BusinessExpert.orgAppList(this, "", LocationXML.getCity(this), String.valueOf(page), String.valueOf(limit), mHandler);
	}

	@Override
	public void onLoadMore() {
		if (expertList != null && expertList.size() >= limit) {
			page++;
			BusinessExpert.orgAppList(this, "", LocationXML.getCity(this), String.valueOf(page), String.valueOf(limit), mHandler);
		}
	}
}
